﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;
using Entities;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;

namespace QLNhaSach.Modules.ThongTin
{
    public partial class ThongTinSach : UserControl
    {
        DataTable table = new DataTable();
        public ThongTinSach()
        {
            InitializeComponent();
            table.Columns.Add("STT");
            table.Columns.Add("Mã sách");
            table.Columns.Add("Tên sách");
            table.Columns.Add("Thể loại");
            table.Columns.Add("Tác giả");
            table.Columns.Add("Đơn giá");
            table.Columns.Add("Số lượng tồn");
            SachBO sbo = new SachBO();
            DataTable dt = sbo.layDanhSachSach();
            int i = 1;
            foreach (DataRow dr in dt.Rows)
            {
                TheLoai tl = new TheLoai();
                TheLoaiBO tlbo = new TheLoaiBO();
                tl = tlbo.GetTheLoaiByID(int.Parse(dr["S_MATHELOAI"].ToString()));
                String tenTheLoai = tl.TenTheLoai;
                TacGia tg = new TacGia();
                TacGiaBO tgbo = new TacGiaBO();
                tg = tgbo.GetTacGiaByID(int.Parse(dr["S_MATACGIA"].ToString()));
                String tenTacGia = tg.TenTacGia;
                table.Rows.Add(i++, dr["S_MASACH"].ToString(), dr["S_TENSACH"].ToString(), tenTheLoai, tenTacGia, dr["S_DONGIA"].ToString(), dr["S_SOLUONGTON"].ToString());
            }
            gridControl1.DataSource = table;
            gridView1.Columns["Mã sách"].Visible = false;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            DataRow cell = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            String value = cell[1].ToString();
            SachBO sbo = new SachBO();
            int kq = sbo.DeleteSach(value.ToString());
            if (kq == 1)
            {
                MessageBox.Show("Xoá sách thành công");
                gridControl1.DataSource = null;
                DataTable dt = new DataTable();
                dt.Columns.Add("STT");
                dt.Columns.Add("Mã sách");
                dt.Columns.Add("Tên sách");
                dt.Columns.Add("Thể loại");
                dt.Columns.Add("Tác giả");
                dt.Columns.Add("Đơn giá");
                dt.Columns.Add("Số lượng tồn");
                DataTable dss = sbo.layDanhSachSach();
                int i = 0;
                foreach (DataRow dr in dss.Rows)
                {
                    TheLoai tl = new TheLoai();
                    TheLoaiBO tlbo = new TheLoaiBO();
                    tl = tlbo.GetTheLoaiByID(int.Parse(dr["S_MATHELOAI"].ToString()));
                    String tenTheLoai = tl.TenTheLoai;
                    TacGia tg = new TacGia();
                    TacGiaBO tgbo = new TacGiaBO();
                    tg = tgbo.GetTacGiaByID(int.Parse(dr["S_MATACGIA"].ToString()));
                    String tenTacGia = tg.TenTacGia;
                    dt.Rows.Add(i++, dr["S_MASACH"].ToString(), dr["S_TENSACH"].ToString(), tenTheLoai, tenTacGia, dr["S_DONGIA"].ToString(), dr["S_SOLUONGTON"].ToString());
                }
                gridControl1.DataSource = dt;
            }else{
                MessageBox.Show("Không thể xoá do có tham chiếu");
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow cell = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (cell != null)
            {
                String value = cell[1].ToString();

                Sach s = new Sach();
                SachBO sbo = new SachBO();
                s = sbo.GetSachByID(int.Parse(value));
                textEdit1.Text = s.TenSach;
                textEdit3.Text = s.SoLuongTon.ToString();
                textEdit4.Text = s.DonGia.ToString();
                textEdit2.Text = s.MaSach.ToString();
                // Load combobox
                RepositoryItemImageComboBox properties = imageComboBoxEdit1.Properties;
                properties.Items.Clear();
                properties.Items.BeginUpdate();
                try
                {
                    TheLoaiBO tlbo = new TheLoaiBO();
                    DataTable dt = tlbo.GetDanhSachTheLoai();
                    foreach (DataRow row in dt.Rows) // Loop over the rows.
                    {
                        //Initialize each item with the display text, value and image index 
                        String tenTheLoai = (String)row["TL_TENTHELOAI"];
                        String maTheLoai = (String)(row["TL_MATHELOAI"].ToString());
                        properties.Items.Add(new ImageComboBoxItem(tenTheLoai, maTheLoai, 0));
                    }
                }
                finally
                {
                    properties.Items.EndUpdate();
                }
                //Select the second item  
                imageComboBoxEdit1.SelectedIndex = s.MaTheLoai - 1;

                RepositoryItemImageComboBox properties2 = imageComboBoxEdit2.Properties;
                properties2 = imageComboBoxEdit2.Properties;
                properties2.Items.Clear();
                properties2.Items.BeginUpdate();
                try
                {
                    TacGiaBO tgbo = new TacGiaBO();
                    DataTable dt = tgbo.GetDanhSachTacGia();
                    foreach (DataRow row in dt.Rows) // Loop over the rows.
                    {
                        //Initialize each item with the display text, value and image index 
                        String tenTacGia = (String)row["TG_TENTACGIA"];
                        String maTacGia = (String)(row["TG_MATACGIA"].ToString());
                        properties2.Items.Add(new ImageComboBoxItem(tenTacGia, maTacGia, 1));
                    }


                }
                finally
                {
                    properties2.Items.EndUpdate();
                }
                //Select the second item  
                imageComboBoxEdit2.SelectedIndex = s.MaTacGia - 1;
            }
            
        }

        private void simpleButton7_Click(object sender, EventArgs e)
        {
            Environment.Exit(1);
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            string tenSach = textEdit1.Text;
            string soLuongTon = textEdit3.Text;
            string donGia = textEdit4.Text;
            string maTheLoai = imageComboBoxEdit1.EditValue.ToString();
            string maTacGia = imageComboBoxEdit2.EditValue.ToString();
            SachBO sbo = new SachBO();
            Sach s = new Sach();
            s.TenSach = tenSach;
            s.MaTheLoai = int.Parse(maTheLoai.ToString());
            s.MaTacGia = int.Parse(maTacGia.ToString());
            s.SoLuongTon = int.Parse(soLuongTon.ToString());
            s.DonGia = int.Parse(donGia.ToString());
            s.MaSach = int.Parse(textEdit2.Text.ToString());
            int kq = sbo.UpdateSach(s);
            if (kq == 1)
            {
                MessageBox.Show("Cập nhật thành công");
                gridControl1.DataSource = null;
                DataTable dt = new DataTable();
                dt.Columns.Add("STT");
                dt.Columns.Add("Mã sách");
                dt.Columns.Add("Tên sách");
                dt.Columns.Add("Thể loại");
                dt.Columns.Add("Tác giả");
                dt.Columns.Add("Đơn giá");
                dt.Columns.Add("Số lượng tồn");
                DataTable dss = sbo.layDanhSachSach();
                int i = 0;
                foreach (DataRow dr in dss.Rows)
                {
                    TheLoai tl = new TheLoai();
                    TheLoaiBO tlbo = new TheLoaiBO();
                    tl = tlbo.GetTheLoaiByID(int.Parse(dr["S_MATHELOAI"].ToString()));
                    String tenTheLoai = tl.TenTheLoai;
                    TacGia tg = new TacGia();
                    TacGiaBO tgbo = new TacGiaBO();
                    tg = tgbo.GetTacGiaByID(int.Parse(dr["S_MATACGIA"].ToString()));
                    String tenTacGia = tg.TenTacGia;
                    dt.Rows.Add(i++, dr["S_MASACH"].ToString(), dr["S_TENSACH"].ToString(), tenTheLoai, tenTacGia, dr["S_DONGIA"].ToString(), dr["S_SOLUONGTON"].ToString());
                }
                gridControl1.DataSource = dt;
            }else{
                MessageBox.Show("Cập nhật thất bại");
            }
        }

        private void simpleButton2_Click_1(object sender, EventArgs e)
        {
            gridControl1.DataSource = null;
            DataTable dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("Mã sách");
            dt.Columns.Add("Tên sách");
            dt.Columns.Add("Thể loại");
            dt.Columns.Add("Tác giả");
            dt.Columns.Add("Đơn giá");
            dt.Columns.Add("Số lượng tồn");
            SachBO sbo = new SachBO();
            DataTable dss = sbo.layDanhSachSach();
            int i = 0;
            foreach (DataRow dr in dss.Rows)
            {
                TheLoai tl = new TheLoai();
                TheLoaiBO tlbo = new TheLoaiBO();
                tl = tlbo.GetTheLoaiByID(int.Parse(dr["S_MATHELOAI"].ToString()));
                String tenTheLoai = tl.TenTheLoai;
                TacGia tg = new TacGia();
                TacGiaBO tgbo = new TacGiaBO();
                tg = tgbo.GetTacGiaByID(int.Parse(dr["S_MATACGIA"].ToString()));
                String tenTacGia = tg.TenTacGia;
                dt.Rows.Add(i++, dr["S_MASACH"].ToString(), dr["S_TENSACH"].ToString(), tenTheLoai, tenTacGia, dr["S_DONGIA"].ToString(), dr["S_SOLUONGTON"].ToString());
            }
            gridControl1.DataSource = dt;
        }
    }
}
